﻿package com.webdesign.action;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webdesign.entity.TbAdmin;
import com.webdesign.entity.TbTeacher;
import com.webdesign.service.ITbAdminService;
import com.webdesign.service.ITbTeacherService;
import com.webdesign.util.MD5;

@Controller("RewritePwdAction")
@Scope("prototype")
public class RewritePwdAction extends ActionSupport{
	private static final String REDIRECT = "redirect";
	private ITbAdminService iTbAdminService;
	private ITbTeacherService iTbTeacherService;
	private String password;
	private String newpwd;
	private String flag;
	private String username;
	
	//跳转到修改密码页面，返回用户名和用户编号
	public String showRewrite(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		String username =  (String)session.get("username");
		return SUCCESS;
	}
	
	//修改密码
	public String rewrite(){
		MD5 md = new MD5();
		String passwordMD5 = md.MD5(password);
		String newpwdMD5 = md.MD5(newpwd);
		Map<String,Object> session = ActionContext.getContext().getSession();
		String user =  (String)session.get("userid");
		TbTeacher tbTeacher = iTbTeacherService.rewrite(user, passwordMD5, newpwdMD5);
		if(tbTeacher != null){
			flag = "success";
			return SUCCESS;
		}else{
			flag = "wrong";
			return SUCCESS;
		}
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getNewpwd() {
		return newpwd;
	}
	
	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

	public ITbAdminService getiTbAdminService() {
		return iTbAdminService;
	}

	@Resource(name="TbAdminService")
	public void setiTbAdminService(ITbAdminService iTbAdminService) {
		this.iTbAdminService = iTbAdminService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	public ITbTeacherService getiTbTeacherService() {
		return iTbTeacherService;
	}

	@Resource(name="TbTeacherService")
	public void setiTbTeacherService(ITbTeacherService iTbTeacherService) {
		this.iTbTeacherService = iTbTeacherService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
}
